package cn.kmust.booth.portal.controller;

import cn.kmust.booth.portal.service.ex.*;
import cn.kmust.booth.portal.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author XChao
 * @date 2020/8/7 - 19:36
 */

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler
    public R handleException(Throwable e) {
        if(e instanceof JobNumberJNullException){
            return R.failure(R.State.ERR_jOB_NUMBER_ISNULL,e);
        }else if(e instanceof PhoneDuplicateException){
            return R.failure(R.State.ERR_PHONE_DUPLICATE,e);
        }else if(e instanceof InsertException){
            return R.failure(R.State.ERR_INSERT,e);
        }else if(e instanceof JobNumberDuplicateException){
            return R.failure(R.State.ERR_JOB_NUMBER_DUPLICATE,e);
        } else if(e instanceof SmsException){
            return R.failure(R.State.ERR_SMS_SEND,e);
        } else if(e instanceof CodeException){
            return R.failure(R.State.ERR_CODE,e);
        }else if(e instanceof PasswordException){
            return R.failure(R.State.ERR_PWD,e);
        }else if(e instanceof UpdateException){
            return R.failure(R.State.ERR_UPDATE,e);
        }else if(e instanceof StallNotFoundException){
            return R.failure(R.State.ERR_STALL_NOT_FOUND,e);
        } else {
            log.debug("Unknown Exception", e);
            return R.failure(R.State.ERR_UNKNOWN, e);
        }
    }
}
